package com.lnj.user.configs;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticClientConfig {

    private static RestClient restClient;// 底层客户端
    private static ElasticsearchTransport transport;// 传输层
    private static ElasticsearchClient client;// 客户端

    /**
     * 获取 ElasticsearchClient 实例
     * @return ElasticsearchClient 实例
     */
    @Bean
    public static ElasticsearchClient elasticsearchClient() {
        if (client == null) {
            restClient = RestClient.builder(
                    new HttpHost("localhost", 9269, "http")
            ).build();
            // 使用 Jackson 映射器创建传输层
            transport = new RestClientTransport(
                    restClient, new JacksonJsonpMapper()
            );
            client = new ElasticsearchClient(transport);
        }
        return client;
    }


}
